package com.rockbb.common.simpleframe.result;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class ResultHandler {

	private HttpServletRequest request;
	private HttpServletResponse response;

	public ResultHandler(HttpServletRequest req, HttpServletResponse res)
	{
		request = req;
		response = res;
	}

	public void process(ActionResult result, Map<String, Object> root) 
	{
		switch (result.getType()) 
		{
			case ActionResult.TYPE_FREEMARKER :
				FreeMarkerProcessor.process(response, result.getValue(), root);
				break;
			case ActionResult.TYPE_JSON :
				if (root.get(ActionResult.MAP_KEY_JSON_CALLBACK) == null)
				{
					JSONProcessor.process(
							response, 
							root.get(ActionResult.MAP_KEY_JSON));
				}
				else
				{
					JSONProcessor.process(
							response, 
							root.get(ActionResult.MAP_KEY_JSON),
							(String)root.get(ActionResult.MAP_KEY_JSON_CALLBACK));
				}
				break;
			case ActionResult.TYPE_RAW :
				RawProcessor.process(response, (String)root.get(ActionResult.MAP_KEY_RAW));
				break;
			case ActionResult.TYPE_REDIRECT :
				RedirectProcessor.process(response, (String)root.get(ActionResult.MAP_KEY_REDIRECT));
				break;
			case ActionResult.TYPE_STREAM :
				StreamProcessor.process(
						response,
						(String)root.get(ActionResult.MAP_KEY_STREAM_TYPE), 
						(String)root.get(ActionResult.MAP_KEY_STREAM_DISPOSITION),
						(String)root.get(ActionResult.MAP_KEY_STREAM_FILENAME), 
						root.get(ActionResult.MAP_KEY_STREAM));
				break;
		}
	}
}
